﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;
using System.Web.UI.HtmlControls;

namespace appClient
{
	public partial class SiteMaster : System.Web.UI.MasterPage
	{
		IClient client;

		protected void Page_Load(object sender, EventArgs e)
		{
			client = new ChannelFactory<IClient>("clientHttp").CreateChannel();
			fillSuppliersList();
		}

		protected void HeadLoginStatus_LoggedOut(Object sender, System.EventArgs e)
		{
			Session.Clear();
		}

		public bool isTheCurrentPage(string pageName)
		{
			if(String.Equals(pageName, System.IO.Path.GetFileNameWithoutExtension(Request.PhysicalPath)))
				return true;
			else
				return false;
		}


		public void fillSuppliersList()
		{
			List<SerializedSupplier> suppliers = new List<SerializedSupplier>();
			suppliers = client.getSuppliers();


			foreach (SerializedSupplier s in suppliers)
			{
				HtmlGenericControl li = new HtmlGenericControl("li");
				if(isTheCurrentPage("Login") || isTheCurrentPage("Register"))
					li.InnerHtml = "<a href=\"../Catalog.aspx?s=" + s.SUPPLIERID.ToString() + "\">" + s.SUPPLIERNAME.ToString() + "</a>";
				else
					li.InnerHtml = "<a href=\"Catalog.aspx?s=" + s.SUPPLIERID.ToString() + "\">" + s.SUPPLIERNAME.ToString() + "</a>";
				leftMenu.Controls.Add(li);
			}
		}
	}
}
